<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */

class RolesController extends AppController {

    /**
     * Define $name
     *
     */
    var $name = 'Roles';
    /**
     * Define $helpers
     *
     */
    var $helpers = array('Html', 'Form');
    /**
     * Define $uses
     *
     */
    var $uses = array('Role');

    /**
     * Describe index
     *
     * @return null
     */
    function index() {  
        $this->Role->recursive = 0;

        // first page
        if (empty($this->params['named']['page']) || empty($this->data)) $this->Session->delete('Search.name');  

        // get the search terms
        if(!empty($this->data['Search']['name'])) $cursearch_role = $this->data['Search']['name'];
        elseif($this->Session->check('Search.name')) $cursearch_role = $this->Session->read('Search.name'); 
        else $cursearch_role = '';

        // construct queries
        $filters = array("Role.deleted=0");
        if(isset($cursearch_role) && $cursearch_role!='') {
            array_push($filters,"Role.name  like '%".$cursearch_role."%'");
            $this->Session->write('Search.name', $cursearch_role);        
        }
        $this->set('roles', $this->paginate('Role',$filters));
        if($this->Auth->user('superuser')){
            $this->set('allow_add_roles',true);
        }
    }

    /**
     * Describe view
     *
     * @param $id
     * @return null
     */
    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid Role.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->set('role', $this->Role->read(null, $id));
    }

    /**
     * Describe add
     *
     * @return null
     */
    function add($committee_id=null) {
        $this->set('committee_id',$committee_id);
        if (!empty($this->data)) {
            $this->Role->create();
            if ($this->Role->save($this->data)) {
                $this->Session->setFlash(__('The Role has been saved', true));
                if($committee_id==null){
                    $this->redirect(array('action'=>'index'));
                }else{
                    $committee=$this->Committee->find('first',array('conditions'=>array('Committee.id'=>$committee_id)));
                    $this->redirect(array('committee'=>$committee['Committee']['short_name'],'controller'=>'wfmodels','action'=>'index','role'=>$this->Role->getLastInsertID()));
                }
            } else {
                $this->Session->setFlash(__('The Role could not be saved. Please, try again.', true));
            }
        }
    }


    /**
     * Describe edit
     *
     * @param $id
     * @return null
     */
    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid Role', true));
            $this->redirect(array('action'=>'index'));
        }
        if (!empty($this->data)) {
            if ($this->Role->save($this->data)) {
                $this->Session->setFlash(__('The Role has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The Role could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Role->read(null, $id);
            $this->set('role',$this->data['Role']);
        }
    }


    /**
     * Describe delete
     *
     * @param $id
     * @return null
     */
    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for Role', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Role->delete($id); 
        $this->Session->setFlash(__('Role deleted', true));
        $this->redirect(array('action'=>'index'));
    }

}
?>
